package com.woniuxy.caradmin.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Insurerecord;
import com.woniuxy.caradmin.entity.pojo.Maintainrecord;
import com.woniuxy.caradmin.entity.pojo.Remind;
import com.woniuxy.caradmin.entity.pojo.Yearcheckrecord;
import com.woniuxy.caradmin.entity.vo.RemindVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.InsurerecordService;
import com.woniuxy.caradmin.service.MaintainrecordService;
import com.woniuxy.caradmin.service.RemindService;
import com.woniuxy.caradmin.service.YearcheckrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/remind")
public class RemindController {
    @Autowired
    private RemindService remindService;
    @Autowired
    private InsurerecordService insurerecordService;
    @Autowired
    private YearcheckrecordService yearcheckrecordService;
    @Autowired
    private MaintainrecordService maintainrecordService;
    @ApiOperation(value = "年检、保养、保险到期检查")
    @GetMapping(value = "/checkExpiretime")
    public R checkExpiretime(){
        boolean flag = remindService.selectExpiretime();
        if(flag){
            return R.ok().code(ResultCode.EXPIRETIMECHECKED).message(Constaint.EXPIRETIMECHECKED);
        }else{
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @ApiOperation(value = "获取到期提醒集合")
    @GetMapping(value = "/selectAllRemind")
    public R selectAllRemind(){
        List<Remind> remindList = remindService.selectAllRemind();
        if (remindList != null&&remindList.size()>0) {
            return R.ok().code(ResultCode.REMINDSUC).message(Constaint.REMINDSUC);
        }else{
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @ApiOperation(value = "获取layui页面的到期提醒")
    @GetMapping(value = "/getRemindList")
    public DataGridView getRemindList(RemindVo remindVo){
        Page<Object> page = PageHelper.startPage(remindVo.getPage(), remindVo.getLimit());
        List<Remind> remindListPage = remindService.selectRemindByPage(remindVo);
        if(remindListPage!=null&&remindListPage.size()>0){
            return new DataGridView(page.getTotal(),remindListPage);
        }else{
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @ApiOperation(value = "批量删除保险提醒记录")
    @GetMapping(value = "/removeRemindByInsure")
    public R removeRemindByInsure(Integer[] ids){
        for (Integer id : ids) {
            Remind remindInsure = new Remind();
            Insurerecord insurerecord = insurerecordService.selectInsureById(id);
            System.out.println(insurerecord);
            remindInsure.setExpiretime(insurerecord.getEndtime());
            remindInsure.setVehiclenumid(insurerecord.getVid());
            remindInsure.setTypee("保险");
            remindService.deleteRemind(remindInsure);
        }
        return R.ok().code(ResultCode.REMINDDELETESUC).message(Constaint.REMINDDELETESUC);
    }
    @ApiOperation(value = "批量删除年检提醒记录")
    @GetMapping(value = "/removeRemindByYearcheck")
    public R removeRemindByYearcheck(Integer[] ids){
        for (Integer id : ids) {
            Remind remindYearcheck = new Remind();
            Yearcheckrecord yearcheckrecord = yearcheckrecordService.selectYearcheckrecordByid(id);
            remindYearcheck.setExpiretime(yearcheckrecord.getNexttime());
            remindYearcheck.setVehiclenumid(yearcheckrecord.getVid());
            remindYearcheck.setTypee("年检");
            remindService.deleteRemind(remindYearcheck);
        }
        return R.ok().code(ResultCode.REMINDDELETESUC).message(Constaint.REMINDDELETESUC);
    }
    @ApiOperation(value = "批量删除保养提醒记录")
    @GetMapping(value = "/removeRemindByMaintain")
    public R removeRemindByMaintain(Integer[] ids){
        for (Integer id : ids) {
            Remind remindMaintain = new Remind();
            Maintainrecord maintainrecord = maintainrecordService.selectMaintainrecordById2(id);
            System.out.println(maintainrecord);
            remindMaintain.setVehiclenumid(maintainrecord.getVid());
            remindMaintain.setExpiretime(maintainrecord.getNexttime());
            remindMaintain.setTypee("保养");
            remindService.deleteRemind(remindMaintain);
        }
        return R.ok().code(ResultCode.REMINDDELETESUC).message(Constaint.REMINDDELETESUC);
    }
}
